#!/bin/sh
# file name mfcommandxml
# modified by Toan Nguyen, Nov 2012

#     Copyright © 2012-2013 Toan Nguyen <toan.nguyen@monash.edu>
#
#     This file is part of Xglom.
# 
#     This is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
# 
#     This is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Function: execute
#
#  This executes an arbitrary command and returns result in xml format.
#
executexml() {
    if test -f "$MFLUX_SID_FILE"
    then {
      MFLUX_SID=`cat "$MFLUX_SID_FILE"`

      java -Djava.net.preferIPv4Stack=true -Dmf.host=$MFLUX_HOST -Dmf.port=$MFLUX_PORT -Dmf.transport=$MFLUX_TRANSPORT -Dmf.sid=$MFLUX_SID -Dmf.result=xml -cp ./aterm.jar arc.mf.command.Execute $*

      RETVAL=$?

      case $RETVAL in 
        3) echo "Session has timed out - need to logon again."; 
           rm -f "$MFLUX_SID_FILE"
        ;;
      esac

    } else {

      echo "Not logged on"

      RETVAL=1      
    }
    fi

}


# check parameters
if [ ! -f ~/.daris_current_parameters ]; then
     echo "Error: cannot find parameters, try logon first!"
     exit 1;
fi
. ~/.daris_current_parameters

# Options:
#
case "$1" in 

  --help)
    echo $"Usage: $0 {<mediaflux service>}"
    RETVAL=1
    ;;

  *) 
    executexml $*
    ;;

esac

exit $RETVAL
